package com.micro.plugins.jwt.configuration;

import com.micro.common.MicroConstant;
import com.micro.common.biz.TokenCacheKeyUtil;
import com.micro.plugins.jwt.base.JWTInfoInterface;
import com.micro.plugins.jwt.helper.JWTHelper;
import com.micro.plugins.jwt.load.JwtLoadConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020/5/11
 */
@Configuration
public class JwtAuthConfiguration {


    private static final String DEFAULT_VERSION = "v1.0.0";

    private static final String CLIENT_APP = "app";

    private static final String CLIENT_M = "msite";

    private static final String CLIENT_WEB = "web";

    private static final String ACCESS_TOKEN = "accessToken";




    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 加载jwt配置信息
     */
    private final JwtLoadConfiguration jwtLoadConfiguration;

    @Autowired
    public JwtAuthConfiguration(JwtLoadConfiguration jwtLoadConfiguration) {
        this.jwtLoadConfiguration = jwtLoadConfiguration;
    }

    /**
     *
     * @param jwtInfo
     * @param expireTime    token过期时间
     * @return              token
     * @throws Exception
     */
    /**
     * <h2>根据jwt信息,过期时间生成token</h2>
     * @param jwtInfo                  {@link JWTInfoInterface} jwt
     * @param tokenType                token类型
     * @return
     * @throws Exception
     */
    public String generateToken(JWTInfoInterface jwtInfo, String tokenType, String clientType) throws Exception {
        if(tokenType.equals(ACCESS_TOKEN)){
            String token = JWTHelper.generateToken(jwtInfo, jwtLoadConfiguration.getUserPriKey(),jwtLoadConfiguration.getMicroJwt().getAccessExpireTime());
            stringRedisTemplate.opsForValue().set(TokenCacheKeyUtil.getAccressTokenKey(jwtInfo.getCustomerCode(),clientType),token,jwtLoadConfiguration.getMicroJwt().getAccessExpireTime(), TimeUnit.SECONDS);
            return token;
        }else {
            String token = JWTHelper.generateToken(jwtInfo, jwtLoadConfiguration.getUserPriKey(),jwtLoadConfiguration.getMicroJwt().getRefreshExpireTime());
            stringRedisTemplate.opsForValue().set(TokenCacheKeyUtil.getRefreshTokenKey(jwtInfo.getCustomerCode(),clientType),token,jwtLoadConfiguration.getMicroJwt().getRefreshExpireTime(), TimeUnit.SECONDS);
            return token;
        }

    }

    /**
     * <h2>通过token获取jwt基础信息</h2>
     * @param token  请求token信息
     * @return {@link JWTInfoInterface} jwt
     */
    public JWTInfoInterface getInfoFromToken(String token) throws Exception {
        return JWTHelper.getInfoFromToken(token, jwtLoadConfiguration.getUserPubKey());
    }

    /**
     * <h2>校验刷新token有效性</h2>
     * @param refreshToken  刷新token
     * @return
     */
    public boolean validataToken(String refreshToken) {
        return JWTHelper.validataToken(refreshToken,jwtLoadConfiguration.getUserPubKey());
    }


    /**
     * <h2>根据渠道类型获取客户端类型</h2>
     * @param channelType
     * @return
     */
    public String findClientType(String channelType) {
        String clientType;
        if(channelType.equals(MicroConstant.ChannelType.MICRO_IOS) || channelType.equals(MicroConstant.ChannelType.MICRO_ANDROID)){
            clientType = CLIENT_APP;
        }else if (channelType.equals(MicroConstant.ChannelType.MICRO_M)){
            clientType = CLIENT_M;
        } else {
            clientType = CLIENT_WEB;
        }
        return clientType;
    }


}
